<template>
  <el-card class="page-card bigCard">
    <template #header>
      <page-tool @showDialog="showDialog" @search="searchFn" />
    </template>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="title"
        label="标题"
        width="250"
      />
      <el-table-column
        prop="content"
        label="内容"
        width="750"
      >
        <template slot-scope="scope">
          <div class="father" v-html="scope.row.content" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template slot-scope="scope">
          <el-row type="flex" justify="center">
            <el-button
              size="mini"
              type="primary"
              @click="updateFn(scope.row.id)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="delFn(scope.row.id)"
            >删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-row type="flex" justify="center">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSet._limit"
        @current-change="currentChange"
      />
    </el-row> -->

    <!-- 分页器 -->
    <MyPagination :page-size="pageSet._limit" :total="total" @getStart="currentChange" />
    <!-- 页面弹窗 -->
    <pages-dialog ref="dialog" :dialog-visible.sync="isShowDialog" @updatePagesList="loadPageList()" @updateCount="loadPageCount" />
  </el-card>

</template>

<script>
import { getPagesList, getPagesCount, delPages, getPagesById } from '@/api/pages'
import pagesDialog from './components/pages-dialog.vue'
import pageTool from './components/pages-tool.vue'
export default {
  components: {
    'pages-dialog': pagesDialog,
    'page-tool': pageTool
  },
  data() {
    return {
      tableData: [{
        title: '',
        content: ''
      }],
      pageSet: {
        _limit: 3,
        _start: 0
      },
      total: 0,
      isShowDialog: false, // 控制弹窗的显示
      searchContent: ''
    }
  },
  created() {
    this.loadPageList()
    this.loadPageCount()
  },
  methods: {
    // 获取页面列表
    async loadPageList(data) {
      let res
      if (data) {
        res = await getPagesList({ ...this.pageSet, title_contains: data })
      } else {
        res = await getPagesList({ ...this.pageSet })
      }
      this.tableData = res.data
    },
    // 获取页面数量
    async loadPageCount(data) {
      if (data) {
        const result = await getPagesCount({ title_contains: data })
        this.total = result.data
      } else {
        const result = await getPagesCount()
        this.total = result.data
      }
    },

    // 切换分页
    currentChange(start) {
      this.pageSet._start = start
      this.loadPageList(this.searchContent)
    },

    // 删除页面
    delFn(id) {
      // 确认框
      this.$confirm('是否删除页面?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPages(id)
        this.loadPageList()
        // 刷新页面数
        this.loadPageCount()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 显示弹窗
    showDialog() {
      this.isShowDialog = true
    },

    // 编辑功能
    async updateFn(id) {
      // 显示弹窗
      this.isShowDialog = true
      // 数据回显
      const res = await getPagesById(id)
      this.$refs.dialog.pageFrom = res.data
    },

    // 筛选功能
    searchFn(contains) {
      this.searchContent = contains
      this.loadPageList(contains)
      // 刷新页面数
      this.loadPageCount(contains)
    }

  }
}
</script>

<style>
 .el-input-group__append {
    background-color: #fff;

}
.page-card {
  margin: 15px 15px;
}
.page-card img {
  height: 200px;
  width: 200px;
}

</style>
